package microcredit.gui.editor.group;

import java.awt.BorderLayout;

import microcredit.gui.editor.AbstractEditorForm;
import microcredit.gui.handler.MemberHandler;
import microcredit.gui.handler.MemberToGroupHandler;
import microcredit.gui.selector.SelectorPanel;
import microcredit.gui.table.impl.MemberToGroupAssignmentTableModel;
import microcredit.model.MemberGroup;
import microcredit.model.MemberToGroup;

public class GroupMemberForm  extends AbstractEditorForm<MemberGroup> {

	private SelectorPanel<MemberToGroup> selector;
	
	public GroupMemberForm() {
		setLayout(new BorderLayout());
		setTitle("Members");
	}
	
	@Override
	public void apply(MemberGroup e) throws Throwable {
		MemberHandler memberHandler = new MemberHandler.ByGroupMemberHandler(this, e, false);
		MemberToGroupHandler memberToGroupHandler = new MemberToGroupHandler(e, memberHandler);
		
		if (selector != null) {
			remove(selector);
		}
		
		selector = new SelectorPanel<MemberToGroup>(MemberToGroup.class, memberToGroupHandler, 
				memberToGroupHandler, 
				memberToGroupHandler, 
				memberToGroupHandler,
				null, memberToGroupHandler, memberToGroupHandler, MemberToGroupAssignmentTableModel.class);
		
		selector.getDeleteAction().setName("Unassign");
		
		add(selector);
	}

	@Override
	public MemberGroup receive(MemberGroup e) throws Throwable {
		// nothing to do
		return e;
	}

}